<template>
  <div class="utility-fee-management">


    <!-- 查询条件 -->
    <el-card class="search-card">
      <el-form :model="queryForm" inline>
        <el-form-item label="社区">
          <el-select v-model="queryForm.communityId" placeholder="请选择社区" clearable>
            <el-option label="测试社区" :value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="费用类型">
          <el-select v-model="queryForm.feeType" placeholder="请选择费用类型" clearable>
            <el-option label="水费" :value="FEE_TYPE.WATER_FEE" />
            <el-option label="电费" :value="FEE_TYPE.ELECTRICITY_FEE" />
          </el-select>
        </el-form-item>
        <el-form-item label="缴费状态">
          <el-select v-model="queryForm.paymentStatus" placeholder="请选择缴费状态" clearable>
            <el-option label="未缴费" :value="0" />
            <el-option label="已缴费" :value="1" />
            <el-option label="逾期" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 操作栏 -->
    <el-card class="operation-card">
      <el-row :gutter="10">
        <el-col :span="1.5">
          <el-button type="primary" @click="handleWaterMeterReading">
            <el-icon><HotWater /></el-icon>
            水表读数
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="success" @click="handleElectricityMeterReading">
            <el-icon><Lightning /></el-icon>
            电表读数
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="warning" @click="handleGenerateWater">
            <el-icon><Plus /></el-icon>
            生成水费
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="info" @click="handleGenerateElectricity">
            <el-icon><Plus /></el-icon>
            生成电费
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger" @click="handleAnomalyCheck">
            <el-icon><Warning /></el-icon>
            异常检测
          </el-button>
        </el-col>
      </el-row>
    </el-card>

    <!-- 水电费列表 -->
    <el-card class="table-card">
      <el-table v-loading="tableLoading" :data="utilityFeeList" stripe>
        <el-table-column prop="id" label="账单ID" width="80" />
        <el-table-column prop="userName" label="用户姓名" min-width="120" />
        <el-table-column prop="householdAddress" label="房户地址" min-width="200" />
        <el-table-column label="费用类型" width="100">
          <template #default="{ row }">
            <el-tag :type="row.feeType === FEE_TYPE.WATER_FEE ? 'primary' : 'success'">
              {{ row.feeType === FEE_TYPE.WATER_FEE ? '水费' : '电费' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="amount" label="费用金额" width="120">
          <template #default="{ row }">
            <span class="amount-text">¥{{ row.amount }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="billingMonth" label="计费月份" width="120" />
        <el-table-column prop="dueDate" label="到期日期" width="120">
          <template #default="{ row }">
            <span :class="getDueDateClass(row.dueDate, row.paymentStatus)">
              {{ formatDate(row.dueDate) }}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="缴费状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getTagType(getPaymentStatusTagType(row.paymentStatus))">
              {{ getPaymentStatusText(row.paymentStatus) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="{ row }">
            <el-button 
              v-if="row.paymentStatus === 0"
              type="primary" 
              size="small" 
              @click="handlePay(row)"
            >
              立即缴费
            </el-button>
            <el-button size="small" @click="handleViewDetail(row)">
              查看详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="pagination.current"
          v-model:page-size="pagination.size"
          :page-sizes="[10, 20, 50, 100]"
          :total="pagination.total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted, watch } from 'vue'
import { showMessage, getTagType } from '@/utils/message'
import { HotWater, Lightning, Plus, Warning } from '@element-plus/icons-vue'
import { utilityFeeApi } from '@/api/property'
import type { UtilityFee, BaseQueryParams } from '@/views/property/types/property'
import { FEE_TYPE } from './types/property'

const tableLoading = ref(false)
const utilityFeeList = ref<UtilityFee[]>([])

const pagination = reactive({
  current: 1,
  size: 10,
  total: 0
})

const queryForm = reactive({
  communityId: undefined as number | undefined,
  feeType: undefined as FEE_TYPE | undefined,
  paymentStatus: undefined as number | undefined,
  billingMonth: ''
})

const feeTypeOptions = [
  { label: '水费', value: FEE_TYPE.WATER_FEE },
  { label: '电费', value: FEE_TYPE.ELECTRICITY_FEE }
]

watch(() => queryForm.feeType, (val) => {
  // 切换费用类型时可自动重置其它筛选条件
  // queryForm.billingMonth = ''
})

const getUtilityFeeList = async () => {
  try {
    tableLoading.value = true
    const params: BaseQueryParams & { feeType?: FEE_TYPE } = {
      current: pagination.current,
      size: pagination.size,
      communityId: queryForm.communityId,
      feeType: queryForm.feeType,
      paymentStatus: queryForm.paymentStatus,
      billingMonth: queryForm.billingMonth
    }
    const response = await utilityFeeApi.getUtilityFeeList(params)
    if (response.code === 200) {
      utilityFeeList.value = response.data.records
      pagination.total = response.data.total
    }
  } catch (error) {
    console.error('获取水电费列表失败:', error)
    showMessage({ type: 'error', message: '获取水电费列表失败' })
  } finally {
    tableLoading.value = false
  }
}

const handleSearch = () => {
  pagination.current = 1
  getUtilityFeeList()
}

const handleReset = () => {
  Object.assign(queryForm, {
    communityId: undefined,
    feeType: undefined,
    paymentStatus: undefined,
    billingMonth: ''
  })
  handleSearch()
}

const handleWaterMeterReading = () => {
  showMessage({ type: 'info', message: '水表读数功能开发中...' })
}

const handleElectricityMeterReading = () => {
  showMessage({ type: 'info', message: '电表读数功能开发中...' })
}

const handleGenerateWater = () => {
  showMessage({ type: 'info', message: '生成水费功能开发中...' })
}

const handleGenerateElectricity = () => {
  showMessage({ type: 'info', message: '生成电费功能开发中...' })
}

const handleAnomalyCheck = () => {
  showMessage({ type: 'info', message: '异常检测功能开发中...' })
}

const handlePay = (row: UtilityFee) => {
  showMessage({ type: 'info', message: '缴费功能开发中...' })
}

const handleViewDetail = (row: UtilityFee) => {
  showMessage({ type: 'info', message: '查看详情功能开发中...' })
}

const handleSizeChange = (size: number) => {
  pagination.size = size
  pagination.current = 1
  getUtilityFeeList()
}

const handleCurrentChange = (current: number) => {
  pagination.current = current
  getUtilityFeeList()
}

const getPaymentStatusTagType = (status: number) => {
  switch (status) {
    case 0: return 'warning'
    case 1: return 'success'
    case 2: return 'danger'
    default: return 'info'
  }
}

const getPaymentStatusText = (status: number) => {
  switch (status) {
    case 0: return '未缴费'
    case 1: return '已缴费'
    case 2: return '逾期'
    default: return '未知'
  }
}

const getDueDateClass = (dueDate: string, paymentStatus: number) => {
  if (paymentStatus === 1) return ''
  const due = new Date(dueDate)
  const now = new Date()
  return due < now ? 'text-danger' : ''
}

const formatDate = (date: string) => {
  if (!date) return ''
  return new Date(date).toLocaleDateString()
}

onMounted(() => {
  getUtilityFeeList()
})
</script>

<style scoped>
.utility-fee-management {
  padding: 20px;
}

.page-header {
  margin-bottom: 20px;
}

.page-header h2 {
  margin: 0 0 8px 0;
  color: #303133;
}

.page-description {
  margin: 0;
  color: #909399;
  font-size: 14px;
}

.search-card {
  margin-bottom: 16px;
}

.operation-card {
  margin-bottom: 16px;
}

.table-card {
  margin-bottom: 16px;
}

.amount-text {
  color: #e6a23c;
  font-weight: bold;
}

.text-danger {
  color: #f56c6c;
}

.pagination-container {
  margin-top: 20px;
  text-align: right;
}
</style> 